﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.orderLinqDAL;
using dhSystemBLL.orderData;
using dhsystem.produceForm;
using baseSet.importExport;
using dhDataAcess.baseBLL;
using baseSet;
using dhDataAcess.sysDAL;

namespace dhsystem.orderForm
{
    public partial class FrmMaterialAdd : Form
    {
        materialListBLL materialList = new materialListBLL();
        materialListModel materialListRow = new materialListModel();
        MaterialTypeBLL materialType = new MaterialTypeBLL();
        stockListBLL stockList = new stockListBLL();
        stockPositionBLL stockPosition = new stockPositionBLL();
        BindingSource BindingSourcematerialAdd;
        string state;
        string type;
        //绑定数据源
        public FrmMaterialAdd(BindingSource bindingSourcematerialList, string _state,string _type)
        {
            InitializeComponent();
            BindingSourcematerialAdd = bindingSourcematerialList;
            state = _state;
            type = _type;
        }
        //打开窗口时绑定数据
        private void FrmMaterialAdd_Load(object sender, EventArgs e)
        {
            
            textMatNum.Focus();
            bindingSourcematerialType.DataSource = materialType.getMaterialTypeAll();
            comboBoxMtype.DataSource = bindingSourcematerialType;
            comboBoxMtype.DisplayMember = "mtype";
            comboBoxMtype.ValueMember = "mtype";
            //bindingSourcestockList.DataSource = stockList.getstockListAll();
            //comboBoxStockName.DataSource = bindingSourcestockList;
            //comboBoxStockName.DisplayMember = "stockName";
            //comboBoxStockName.ValueMember = "stockName";
            bindingSourcestockPosition.DataSource = stockPosition.getstockPositionAll();
            comboBoxPosName.DataSource = bindingSourcestockPosition;
            comboBoxPosName.DisplayMember = "posName";
            comboBoxPosName.ValueMember = "posName";
            if (type == "五金")
            {
                comboBoxMtype.Text = "五金";
                textStockName.Text = "五金仓";
                comboBoxunit.Items.Clear();
                comboBoxunit.Items.Add("个");
                comboBoxunit.Items.Add("件");
                comboBoxunit.Items.Add("张");
                comboBoxunit.Items.Add("本");
                comboBoxunit.Text = "个";
                this.Text = "新增五金资料";
                textMatNum.Text = "WJ" + "-" + basicSql.getOrderSerail("五金编号");
            }
            if (state == "编辑")
            {
                textMatNum.DataBindings.Add("text", BindingSourcematerialAdd, "matNum");
                comboBoxMtype.DataBindings.Add("text", BindingSourcematerialAdd, "mtype");
                textMatName.DataBindings.Add("text", BindingSourcematerialAdd, "matName");
                comboBoxMatSpec.DataBindings.Add("text", BindingSourcematerialAdd, "matSpec");
                comboBoxMatPord.DataBindings.Add("text", BindingSourcematerialAdd, "matPord");
                textsupplier.DataBindings.Add("text", BindingSourcematerialAdd, "supplier");
                comboBoxunit.DataBindings.Add("text", BindingSourcematerialAdd, "unit");
                comboBoxFzunit.DataBindings.Add("text", BindingSourcematerialAdd, "Fzunit");
                textStockName.DataBindings.Add("text", BindingSourcematerialAdd, "stockName");
                comboBoxPosName.DataBindings.Add("text", BindingSourcematerialAdd, "posName");
                textMatLot.DataBindings.Add("text", BindingSourcematerialAdd, "matLot");
                textCreateUser.DataBindings.Add("text", BindingSourcematerialAdd, "createUser");
                textUpdateUser.DataBindings.Add("text", BindingSourcematerialAdd, "updateUser");
                textmark.DataBindings.Add("text", BindingSourcematerialAdd, "mark");
                textprice.DataBindings.Add("text", BindingSourcematerialAdd, "price");
            }
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (state == "新增")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否新增原材料资料！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        
                        materialListRow.matNum = textMatNum.Text;
                        materialListRow.mtype = comboBoxMtype.Text;
                        materialListRow.matName = textMatName.Text;
                        materialListRow.matSpec = comboBoxMatSpec.Text;
                        materialListRow.matpord = comboBoxMatPord.Text;
                        materialListRow.unit = comboBoxunit.Text;
                        materialListRow.Fzunit = comboBoxFzunit.Text;
                        materialListRow.matLot = textMatLot.Text;
                        materialListRow.stockName = textStockName.Text;
                        materialListRow.posName = comboBoxPosName.Text;
                        materialListRow.createDate = DateTime.Now;
                        materialListRow.createUser = textCreateUser.Text;
                        materialListRow.updateDate = DateTime.Now;
                        materialListRow.updateUser = textUpdateUser.Text;
                        materialListRow.mark = textmark.Text;
                        if (textprice.Text == "")
                        {
                            materialListRow.price = 0;
                        }
                        else
                        {
                            materialListRow.price = Convert.ToDecimal (textprice.Text);
                        }
                        materialListRow.supplier = textsupplier.Text;
                        if (textprice.Text == "")
                        {
                            materialListRow.price = 0;
                        }
                        else
                        {
                            materialListRow.price = Convert.ToDecimal(textprice.Text);
                        }
                        
                        materialList.materialListInsert(materialListRow,type );
                       
                        bindingSourcematerialList.DataSource = materialList.getmaterialListAll();
                        MessageBox.Show("新增成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
            if (state == "编辑")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否修改原材料资料！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        materialListRow.matNum = textMatNum.Text;
                        materialListRow.mtype = comboBoxMtype.Text;
                        materialListRow.matName = textMatName.Text;
                        materialListRow.matSpec = comboBoxMatSpec.Text;
                        materialListRow.matpord = comboBoxMatPord.Text;
                        materialListRow.unit = comboBoxunit.Text;
                        materialListRow.Fzunit = comboBoxFzunit.Text;
                        materialListRow.matLot = textMatLot.Text;
                        materialListRow.stockName = textStockName.Text;
                        materialListRow.posName = comboBoxPosName.Text;
                        materialListRow.createDate = DateTime.Now;
                        materialListRow.createUser = textCreateUser.Text;
                        materialListRow.updateDate = DateTime.Now;
                        materialListRow.updateUser = textUpdateUser.Text;
                        materialListRow.mark = textmark.Text;
                        materialListRow.supplier = textsupplier.Text;
                        if (textprice.Text == "")
                        {
                            materialListRow.price = 0;
                        }
                        else
                        {
                            materialListRow.price = Convert.ToDecimal (textprice.Text);
                        }
                        materialList.materialListUpdate (materialListRow);
                        bindingSourcematerialList.DataSource = materialList.getmaterialListAll();
                        MessageBox.Show("修改成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttselectcustNum_Click(object sender, EventArgs e)
        {
            string field;
            field = "mtype";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.MaterialType , field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxMtype.SelectedValue = productselect.pname;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string field;
            field = "posName";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.stockPosition , field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxPosName.SelectedValue = productselect.pname;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string field;
            field = "sName";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.supplierList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                textsupplier.Text  = productselect.pname;
            }
        }

    
    }
}
